package com.cuoyi.coupon.mall.controller;

import cn.hutool.core.convert.Convert;
import com.cuoyi.coupon.mall.common.ApiResult;
import com.cuoyi.coupon.mall.common.BusinessException;
import com.cuoyi.coupon.mall.common.Entry;
import com.cuoyi.coupon.mall.enums.ErrorEnum;
import com.cuoyi.coupon.mall.model.req.UserReq;
import com.cuoyi.coupon.mall.mybatis.entity.User;
import com.cuoyi.coupon.mall.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author zhang.cheng
 * 2022-02-17 14:32
 **/
@RestController
@Api(value = "测试接口", tags = "用户管理相关的接口")
@RequestMapping(value = Entry.ADMIN + "/user")
public class UserController {

    @Resource
    private UserService userService;

    @PostMapping(value = "/save")
    @ApiOperation(value = "添加用户", notes = "添加用户")
    public ApiResult saveUser(@Valid @RequestBody UserReq userReq) {
        if (StringUtils.isEmpty(userReq.getName())) {
            throw new BusinessException(ErrorEnum.PARAMS_ERROR.code(), "用户名不能为空！");
        }
        userService.save(Convert.convert(User.class, userReq));
        return ApiResult.ok();
    }

    @GetMapping(value = "findById")
    @ApiOperation(value = "根据id获取用户信息", notes = "根据id查询用户信息")
    public ApiResult<User> getUser(Long id) {
        return ApiResult.ok(userService.findById(id));
    }
}
